<template>
  <div class="page">
    <v-pageTitle title="项目管理"></v-pageTitle>
    <v-pageSearch>
      <el-row style="width: 100%;">
        <el-form :inline="true">
          <el-col :span="16">
            <el-form-item label="项目名称">
              <el-input
                size='small'
                placeholder="请输入查询关键字"
                icon="search"
                v-model="input"
                :on-icon-click="handleIconClick">
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item style="float: right;">
              <el-button  type="danger" :disabled="this.sels.length===0" @click="del" size="small">删除</el-button>
            </el-form-item>
            <el-form-item style="float: right;">
              <el-button  type="primary" @click="batchAdd" size="small">新增项目</el-button>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
    </v-pageSearch>
    
    <v-pageTable pagination :sizeChange="sizeChange" :pageChange="pageChange" :paginationTotal="paginationTotal" paginationAlign="center">
      <el-table v-loading="tableData.loading" :data="tableData.body" border style="width: 100%" @selection-change="selsChange" :default-sort="{prop: 'date', order: 'descending'}">
          <el-table-column fixed type="selection" width="50">
          </el-table-column>
          <el-table-column type="index" label="序号" width="120" align="center">
          </el-table-column>
          <el-table-column prop="flag" label="项目编号" min-width="120" sortable>
          </el-table-column>
          <el-table-column prop="name" label="项目名称" min-width="120" sortable>
          </el-table-column>
          <el-table-column prop="branch" label="所属部门" min-width="100" sortable>
          </el-table-column>
          <el-table-column prop="remark" label="备注" min-width="100" sortable>
          </el-table-column>
          <el-table-column label="操作" min-width="150">
              <template slot-scope="scope">
                <el-button  @click="batchEdit(scope.row)" type="primary" size="small">编辑</el-button>
                <el-button  @click="handleDel(scope.$index, scope.row)" type="danger" size="small">删除</el-button>
              </template>
          </el-table-column>
      </el-table>
    </v-pageTable>

    <!--编辑界面-->
    <el-dialog title="编辑" v-model="editFormVisible" :close-on-click-modal="false">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="80px" class="demo-ruleForm">
        <el-form-item label="项目编号" prop="flag">
          <el-input v-model="ruleForm.flag"></el-input>
        </el-form-item>
        <el-form-item label="项目名称" prop="name">
          <el-input v-model="ruleForm.name"></el-input>
        </el-form-item>
        <el-form-item label="所属部门" prop="branch">
          <el-select v-model="ruleForm.branch" filterable placeholder="请选择">
            <el-option
              v-for="item in branch_options"
              :key="item.branchId"
              :label="item.branch"
              :value="item.branchId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 4}"
            placeholder="请输入内容"
            v-model="ruleForm.remark">
          </el-input>
        </el-form-item>
        <el-form-item label="当前时间">
            <h3 style="font-weight:400;">{{nowDate}}</h3>
        </el-form-item>
        <el-form-item>
            <el-button  type="primary" @click.native="submitForm('ruleForm')" :loading="editLoading">提交</el-button>
            <el-button  @click.native="editFormVisible = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import util from '../../util/util'
export default {
    data() {
      return {
        input: '',
        tableData: {
            loading: true,
            head: [],
            body: []
        },
        paginationTotal: 100,
        sels:[],

        //编辑界面数据
		    ruleForm: {
          flag: '',
          name: '',
          branch: '',
          remark: ''
        },
        branch_options: [
          {
            branchId: '1',
            branch: '系统平台部',
            flag: '130000001',
            master: '郑山',
            remark: '物联网平台开发',
            status: true
          }, {
            branchId: '2',
            branch: '移动终端部',
            flag: '130000002',
            master: '吴先锋',
            remark: 'PDA终端开发',
            status: true
          }
        ],
        rules: {
          flag: [
            { required: true, message: '请输入项目编号', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '请输入项目名称', trigger: 'blur' }
          ],
          branch: [
            { required: true, message: '请选择项目类型', trigger: 'blur' }
          ]
        },
        editFormVisible: false,//编辑界面是否显示
        editLoading: false,

        nowDate: util.formatDate.format(new Date(), 'yyyy-MM-dd hh:mm:ss')
      }
    },
    filters: {
      statusFilter(statue) {
        let sta = statue ? 0 : 1;
        let statusMap = ['禁用','启用']
        return statusMap[sta]
      },
      statusFilterType(statue) {
        let sta = statue ? 0 : 1;
        let statusMapType = ['success','error']
        return statusMapType[sta]
      },
      genderFilter(statue) {
        let statusMap = ['未知','男','女']
        return statusMap[statue]
      },
      genderFilterType(statue) {
        let statusMapType = ['primary','error','success']
        return statusMapType[statue]
      }
    },
    methods: {
      handleIconClick(){
        console.log(this.input);
      },
      getTableData(data) {
          // ajax 查询表格数据
          this.tableData.body = [
              {
                  name: '韵达项目',
                  flag: '310000001',
                  branch: '系统平台部',
                  remark: '韵达收银系统',
                  branchId: '1'
              }, {
                  name: 'PDA项目',
                  flag: '310000002',
                  branch: '移动终端部',
                  branchId: '2',
                  remark: '。。。。'
              }, {
                  name: 'webApp项目',
                  flag: '310000003',
                  remark: 'H5移动开发',
                  branch: '系统平台部',
                  branchId: '1'
              }
          ]
          // 模拟加载
          setTimeout(() => {
              this.tableData.loading = false
          }, 1000)
      },
      sizeChange(val){
          console.log(val)
          this.getTableData()
      },
      pageChange(val){
          console.log(val)
          this.getTableData()
      },
      selsChange: function (sels) {
          this.sels = sels;
      },
      handleClick(row) {
          console.log(row);
      },
      //显示新增界面
      batchAdd(){
          this.editFormVisible = true;
      },
      //显示编辑界面
      batchEdit: function (row) {
          this.editFormVisible = true;
          this.ruleForm = Object.assign({}, row);
      },
      batchClick(){
          // var ids = this.sels.map(item => item.id).toString();
          console.log(this.sels)
      },
      submitForm(formName) {
          this.$refs[formName].validate((valid) => {
          if (valid) {
              alert('submit!');
              this.editFormVisible = false;
          } else {
              console.log('error submit!!');
              return false;
          }
          });
      },
      resetForm(formName) {
          this.$refs[formName].resetFields();
      },
      //删除
      handleDel: function (index, row) {
          this.$confirm('确认删除该项目吗?', '提示', {
              type: 'warning'
          }).then(() => {
              this.listLoading = true;
              //NProgress.start();
              let para = { id: row.id };
              removeUser(para).then((res) => {
                  this.listLoading = false;
                  //NProgress.done();
                  this.$message({
                      message: '删除成功',
                      type: 'success'
                  });
                  this.getUsers();
              });
          }).catch(() => {

          });
      },
      del: function (index, row) {
          this.$confirm('确认删除该项目吗?', '提示', {
              type: 'warning'
          }).then(() => {
              this.listLoading = true;
              //NProgress.start();
              let para = { id: row.id };
              removeUser(para).then((res) => {
                  this.listLoading = false;
                  //NProgress.done();
                  this.$message({
                      message: '删除成功',
                      type: 'success'
                  });
                  this.getUsers();
              });
          }).catch(() => {

          });
      }
    },
    watch: {
      //第一种写法 适用于普通变量（简单类型的值的观测写法）
      // 关闭dialog的时候 初始化页面表单数据
      editFormVisible: function (val, oldVal) {
        console.log('new: %s, old: %s', val, oldVal)
        if(!val){
          console.log(this.ruleForm)
          this.ruleForm = {
            flag: '',
            name: '',
            branch: '',
            remark: ''
          }
        }
      }
    },
    mounted() {
        // 获取table数据
        this.getTableData()

        let  _this = this;
        this.timer = setInterval(function(){
            _this.nowDate = util.formatDate.format(new Date(), 'yyyy-MM-dd hh:mm:ss');
        },1000);
    },
    beforeDestory(){
        if(this.timer){
            clearInterval(this.timer)
        }
    }
  }
</script>